// 引入express模块
const express = require('express')
//创建路由器对象
const r = express.Router();
// 引入数据库连接池
const pool = require('../pool.js')

//添加学生用户注册接口（post/reg）
//接口地址：http://127.0.0.1:8080/user/reg
//请求方式:post
r.post('/reg', (req, res, next) => {
	//1.1获取post传递的参数
	let obj = req.body;
	console.log(obj);
	//1.2验证传递的参数是否为空
	if (obj.uname === '') { //验证用户名
		res.send({
			code: 401,
			msg: '用户名不能为空'
		});
		//阻止往后执行
		return;
	}
	if (!/^1[3-9]\d{9}$/.test(obj.phone)) { //验证手机
		res.send({
			code: 404,
			msg: '手机格式错误'
		});
		return;
	}
	if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(obj.email)) { //验证邮箱
		res.send({
			code: 403,
			msg: '邮箱格式错误'
		});
		return;
	}
	pool.query('insert into stu_tab (stu_name,stu_pwd,stu_phone,stu_mail) values(?,?,?,?)', [obj.uname, obj
		.upwd, obj.phone, obj.email
	], (err, result) => {
		console.log(err)
		if (err) {
			return; //阻止往后执行
		}
		res.send({
			code: 200,
			msg: '添加成功'
		});
	});
});
//添加学生信息接口
r.post('/addstudent', (req, res) => {
	//1.1获取post传递的参数
	let obj = req.body;
	console.log(obj);
	pool.query('insert into stu_tab (stu_name,stu_pwd,stu_age,stu_gender,stu_phone,stu_mail,stu_sign) values(?,?,?,?,?,?,?)', [obj.stu_name, obj
		.stu_pwd, obj.stu_age, obj.stu_gender,obj.stu_phone,obj.stu_mail,obj.stu_sign
	], (err, result) => {
		console.log(err)
		if (err) {
			return; //阻止往后执行
		}
		res.send({
			code: 200,
			msg: '注册成功'
		});
	});
})


//添加修改学生信息接口
r.post('/regUpdata', (req, res) => {
	console.log(req.body)
	let obj = req.body
	let age = parseInt(obj.age)
	if (obj.user === '') { //验证用户名
		res.send({
			code: 401,
			msg: '用户名不能为空'
		});
		//阻止往后执行
		return;
	}
	if (age <= 0 && age >= 150) { //年龄验证
		res.send({
			code: 402,
			msg: '年龄不能小于0或大于150'
		});
		return
	}
	pool.query('update stu_tab set stu_name=?,stu_age=?,stu_gender=?,stu_sign=? where stu_id=?', [obj.user, obj
		.age, obj.sex, obj.geqian, obj.stu_id
	], (err, result) => {
		if (err) {
			return; //阻止往后执行
		}
		res.send({
			code: 200,
			msg: '修改成功'
		});
	});
})

//添加学生用户登录接口（post/login）
//接口地址：http://127.0.0.1:8080/user/login
//请求方式:post
r.post('/login', (req, res) => {
	var obj = req.body;
	console.log(obj);
	if (obj.uname === '') {
		res.send({
			code: 401,
			msg: '用户名不能为空'
		})
		return;
	}
	if (obj.upwd === '') {
		res.send({
			code: 402,
			msg: '密码不能为空'
		})
		return;
	}
	pool.query('select * from stu_tab where stu_name=? and stu_pwd=?', [obj.uname, obj.upwd], (err, result) => {
		if (err) {
			return;
		}
		console.log(result)
		if (result.length == 0) {
			res.send({
				code: 501,
				msg: '登录失败'
			});
		} else {
			res.send({
				code: 200,
				msg: '登录成功',
				result
			});
		}
	})
})

//添加查找讲师信息接口
r.get('/selectTeacher', (req, res) => {
	sql = 'select * from tutor_tab'
	pool.query(sql, (err, result) => {
		if (err) {
			return
		} else {
			res.send({
				result
			})
		}
	})
})
//添加删除教师信息接口
r.delete('/deleteTeacher', (req, res) => {
	console.log(req.query.tutor_id)
	let tutor_id = req.query.tutor_id
	pool.query('delete from tutor_tab where tutor_id=?',tutor_id, (err, result) => {
		if (err) return;
		res.send({
			code: 200,
			msg: '删除成功'
		})
	})
})
//添加讲师用户注册接口（post/reg_tutor）
//接口地址：http://127.0.0.1:8080/user/reg_tutor
//请求方式:post
r.post('/reg_tutor', (req, res, next) => {
	//1.1获取post传递的参数
	let obj = req.body;
	console.log(obj);
	//1.2验证传递的参数是否为空
	if (obj.uname === '') { //验证用户名
		res.send({
			code: 401,
			msg: '用户名不能为空'
		});
		//阻止往后执行
		return;
	}
	if (!/^1[3-9]\d{9}$/.test(obj.phone)) { //验证手机
		res.send({
			code: 404,
			msg: '手机格式错误'
		});
		return;
	}
	if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(obj.email)) { //验证邮箱
		res.send({
			code: 403,
			msg: '邮箱格式错误'
		});
		return;
	}
	pool.query('insert into tutor_tab (tutor_name,tutor_pwd,tutor_phone,tutor_mail) values(?,?,?,?)', [obj
		.uname, obj.upwd, obj.phone, obj.email
	], (err, result) => {
		console.log(result)
		if (err) {
			return; //阻止往后执行
		}
		res.send({
			code: 200,
			msg: '注册成功'
		});
	});
});

//添加讲师用户登录接口（post/login_tutor）
//接口地址：http://127.0.0.1:8080/user/login_tutor
//请求方式:post
r.post('/login_tutor', (req, res) => {
	let obj = req.body;
	console.log(obj);
	if (obj.uname === '') {
		res.send({
			code: 401,
			msg: '用户名不能为空'
		})
		return;
	}
	if (obj.upwd === '') {
		res.send({
			code: 402,
			msg: '密码不能为空'
		})
		return;
	}
	pool.query('select * from tutor_tab where tutor_name=? and tutor_pwd=?', [obj.uname, obj.upwd], (err,
		result) => {
		if (err) {
			return;
		}
		console.log(result)
		if (result.length == 0) {
			res.send({
				code: 501,
				msg: '登录失败'
			});
		} else {
			res.send({
				code: 200,
				msg: '登录成功'
			});
		}
	})
})
//添加管理员用户登录接口（post/login_admin）
//接口地址：http://127.0.0.1:8080/user/login_admin
//请求方式:post
r.post('/login_admin', (req, res) => {
	let obj = req.body;
	console.log(obj);
	if (obj.uname === '') {
		res.send({
			code: 401,
			msg: '用户名不能为空'
		})
		return;
	}
	if (obj.upwd === '') {
		res.send({
			code: 402,
			msg: '密码不能为空'
		})
		return;
	}
	pool.query('select * from admin_tab where admin_Account=? and admin_pwd=?', [obj.uname, obj.upwd], (err,
		result) => {
		if (err) {
			return;
		}
		console.log(result)
		if (result.length == 0) {
			res.send({
				code: 501,
				msg: '登录失败'
			});
		} else {
			res.send({
				code: 200,
				msg: '登录成功'
			});
		}
	})
})

//添加验证旧密码接口
r.get('/pwdYanzheng', (req, res) => {
	console.log(req.query.pwd_j)
	sql = `select stu_pwd from stu_tab where stu_id=${req.query.stu_id}`
	pool.query(sql, (err, result) => {
		console.log(err)
		if (err) {
			return; //阻止往后执行
		}
		//去掉RowDataPacket，变为数组里面包含对象
		let res1 = JSON.parse(JSON.stringify(result));
		// 拿出数据
		console.log(res1[0].stu_pwd)
		console.log(req.query.pwd_j)
		if (req.query.pwd_j == res1[0].stu_pwd) {
			res.send({
				code: 200,
				msg: '密码正确'
			});
		} else {
			res.send({
				code: 403,
				msg: '密码错误'
			});
		}

	})
})
//添加修改新密码接口
r.post('/pwdUpdate', (req, res) => {
	console.log(req.body)
	let r = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/
	if (r.test(req.body.pwd_x)) {
		pool.query('update stu_tab set stu_pwd=? where stu_id=?', [req.body.pwd_x, req.body.stu_id], (err,
			result) => {

			console.log(err)
			if (err) {
				return; //阻止往后执行
			}
			res.send({
				code: 200,
				msg: '修改成功'
			});
		})
	} else {
		res.send({
			code: 402,
			msg: '至少8个字符，至少1个大写字母，1个小写字母和1个数字'
		})
		return;
	}
})
//添加修改新邮箱接口
r.post('/emailUpdate', (req, res) => {
	console.log(req.body)
	let reg4 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
	if (reg4.test(req.body.youxiang)) {
		pool.query('update stu_tab set stu_mail=? where stu_id=?', [req.body.youxiang, req.body.stu_id], (err,
			result) => {
			console.log(err)
			if (err) {
				return; //阻止往后执行
			}
			res.send({
				code: 200,
				msg: '邮箱修改成功'
			});
		})
	} else {
		res.send({
			code: 402,
			msg: '请输入合法邮箱'
		})
		return;
	}
})
//添加修改新手机号接口
r.post('/phonelUpdate', (req, res) => {
	console.log(req.body)
	let reg3 = /^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$/
	if (reg3.test(req.body.shoujihao)) {
		pool.query('update stu_tab set stu_phone=? where stu_id=?', [req.body.shoujihao, req.body.stu_id], (err,
			result) => {
			console.log(err)
			if (err) {
				return; //阻止往后执行
			}
			res.send({
				code: 200,
				msg: '手机号修改成功'
			});
		})
	} else {
		res.send({
			code: 402,
			msg: '请输入合法手机号'
		})
		return;
	}
})
//添加查询学生信息接口
r.get('/selectStudent', (req, res) => {
	sql = 'select * from stu_tab'
	pool.query(sql, (err, result) => {
		if (err) {
			return
		} else {
			res.send({
				result
			})
		}
	})
})
// 添加删除学生信息
r.delete('/deleteStuden', (req, res) => {
	console.log(req.query.stu_id)
	let stu_id = req.query.stu_id
	pool.query('delete from stu_tab where stu_id=?', stu_id, (err, result) => {
		if (err) return;
		res.send({
			code: 200,
			msg: '删除成功'
		})
	})
})

// 导出路由器对象
module.exports = r;
